<?php
/**
 * 毛文科
 */
namespace app\admin\model;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xls;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Phpspreadsheet
{
    protected $type = 'Xlsx';
    protected $columns = [];

    /**设置类型
     * @param $type
     * @return $this
     */
    public function setType($type){
        $this->type = $type;
        return $this;
    }

    /**设置列
     * @param $key
     * @param $value
     * @param null $width
     * @return $this
     */
    public function setColumns($key,$value,$type='text',$opeion=['开启','关闭'],$width=null){
        $this->columns[] = [
            'key'=>$key,
            'value'=>$value,
            'width'=>$width,
            'type'=>$type,
            'option'=>$opeion
        ];
        return $this;
    }

    /**
     * @param array $array
     * @param null $name
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    public function setData($array=[],$name=null){
        $spreadsheet  = new Spreadsheet();
        $sheet  = $spreadsheet->getActiveSheet();
        $count = count($this->columns);
        $shuju = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',];
//        $sheet->setCellValue('A1','你好');
        foreach ($this->columns as $newkey=>$value){
            $sheet->setCellValue($shuju[$newkey].'1',$this->columns[$newkey]['value']);
        }
        foreach ($array as $key=>$value){
            foreach ($this->columns as $k=>$item){
                if($item['type']=='text'||$item['type']=='switch'){
                    $sheet->setCellValue($shuju[$k].($key+2),$value[$item['key']]??'');
                }elseif ($item['type']=='time'){
                    $sheet->setCellValue($shuju[$k].($key+2),date('Y-m-d H:i',$value[$item['key']]??0));
                }
                if($item['width']){
                    $spreadsheet->getActiveSheet()->getColumnDimension($shuju[$k])->setWidth($item['width']);
                }
            }
        }
        if(!$name){
            $name = time();
        }
        if($this->type=='Xlsx'){
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
            header('Cache-Control: max-age=0');
            $writer = new Xlsx($spreadsheet);
            $writer->save('php://output');

            //删除清空：
            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);
            exit;
        }else{
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . $name . '.xls"');
            header('Cache-Control: max-age=0');
            $writer = new Xls($spreadsheet);
            $writer->save('php://output');

            //删除清空：
            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);
            exit;
        }
    }
}
